package com.syqc.utils

import android.annotation.SuppressLint
import com.syqc.comlib.utils.MLog
import com.syqc.entity.AlarmSub
import com.syqc.entity.AlarmType
import com.syqc.monitor.ui.alarm.ReportSetModel
import com.syqc.net.ServiceHelper

/**
 *@Author Jasper
 *@Time 2021/11/20 17:12
 *@Desc
 */
class AlarmDataUtil {
    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            AlarmDataUtil()
        }
    }

    fun init() {
        getNormalAlarm()
        getAdasAlarm()
    }


    @SuppressLint("CheckResult")
    fun getNormalAlarm() {
        ServiceHelper.getRepository().getAlarmType(false).subscribe({
            if (it.isNullOrEmpty()) return@subscribe

            handleAlarms(it, false)
        }, {
            MLog.e("获取报警类型数据失败")
        })
    }


    @SuppressLint("CheckResult")
    fun getAdasAlarm() {
        ServiceHelper.getRepository().getAlarmType(true).subscribe({
            if (it.isNullOrEmpty()) return@subscribe
            handleAlarms(it, true)
        }, {
            MLog.e("获取主动安全报警类型数据失败")
        })
    }


    /**
     * 获取默认数据
     */
    fun getCarDefaultAlarm(isAdas: Boolean) {
        if (isAdas) {
            setChildChoose(true, "adas8Alarm", true)
            setChildChoose(true, "adas7Alarm", true)
        } else {
            setChildChoose(false, "importantAlarm", true)
            setChildChoose(false, "adas7Alarm", true)
        }

        val chooseIds = getChooseIds(isAdas)
        if (isAdas) {
            ReportSetModel.instance.setAdasSize(totalAdas)
            ReportSetModel.instance.setAdasAlarmIds(chooseIds)
        } else {
            ReportSetModel.instance.setNormalSize(total)
            ReportSetModel.instance.setNormalAlarmIds(chooseIds)
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    private val allAlarm = mutableListOf<AlarmSub>()
    private val allAdasAlarm = mutableListOf<AlarmSub>()
    private val likeData = mutableListOf<AlarmSub>()


    fun getAllAlarms(isAdas: Boolean) = if (isAdas) allAdasAlarm else allAlarm

    private fun handleAlarms(alarms: ArrayList<AlarmType>, isAdas: Boolean) {
        if (isAdas) allAdasAlarm.clear() else allAlarm.clear()
        var index = 0
        alarms.forEach {
            index++
            val alarm = AlarmSub(it.id, it.name, false, "A0", index, 1)
            if (isAdas) allAdasAlarm.add(alarm) else allAlarm.add(alarm)
            it.children.forEach { item ->
                index++
                val alarmSub = AlarmSub(item.id, item.name, false, it.id, index, 2)
                if (isAdas) allAdasAlarm.add(alarmSub) else allAlarm.add(alarmSub)
            }
        }
        if (isAdas) totalAdas = 0 else total = 0
        if (isAdas) alarms.forEach {
            it.children.forEach { _ ->
                totalAdas++
            }
        }
        else alarms.forEach {
            it.children.forEach { _ ->
                total++
            }
        }
    }

    /**
     * 关键词筛选数据
     *
     * @param key
     * @return
     */
    fun getDataLike(isAdas: Boolean, key: String): MutableList<AlarmSub> {
        likeData.clear()
        val data = if (isAdas) allAdasAlarm else allAlarm
        val upKey = key.uppercase()
        data.forEach {
            if (it.itemType == 2 && it.name.uppercase().contains(upKey)) {
                likeData.add(it)
            }
        }
        return likeData
    }

    fun resetAlarm(isAdas: Boolean) {
        val data = if (isAdas) allAdasAlarm else allAlarm

        data.forEach { item ->
            item.isChoose = false
        }
    }

    /**
     * 设置选中状态
     *
     * @param ids
     */
    fun getChooseByIds(isAdas: Boolean, ids: String): MutableList<AlarmSub> {
        if (isAdas) {
            ids.split(",").forEach {
                allAdasAlarm.forEach { item ->
                    if (it == item.id) {
                        item.isChoose = true
                    }
                }
            }

            allAdasAlarm.forEach {
                if (it.itemType == 1) {
                    it.isChoose = isPChoose(true, it.id)
                }
            }
        } else {
            ids.split(",").forEach {
                allAlarm.forEach { item ->
                    if (it == item.id) {
                        item.isChoose = true
                    }
                }
            }

            allAlarm.forEach {
                if (it.itemType == 1) {
                    it.isChoose = isPChoose(false, it.id)
                }
            }
        }

        return if (isAdas) allAdasAlarm else allAlarm
    }

    /**
     * @param id
     * @param isChoose
     */
    fun setChildChoose(isAdas: Boolean, id: String, isChoose: Boolean) {
        val data = if (isAdas) allAdasAlarm else allAlarm
        data.forEach {
            if (id == it.pid) it.isChoose = isChoose
        }
    }

    /**
     * 是否全选/
     *
     * @return
     */
    fun isAllChoose(isAdas: Boolean): Boolean {
        val data = if (isAdas) allAdasAlarm else allAlarm
        data.forEach {
            if (!it.isChoose) return false
        }
        return true
    }

    /***
     * 是否全部选
     */
    fun isAllNoChoose(isAdas: Boolean): Boolean {
        val data = if (isAdas) allAdasAlarm else allAlarm
        data.forEach {
            if (it.isChoose) return false
        }
        return true
    }

    fun setAllChoose(isAdas: Boolean, isChoose: Boolean) {
        val data = if (isAdas) allAdasAlarm else allAlarm
        data.forEach {
            it.isChoose = isChoose
        }
    }

    /**
     * 设置父类是否选中
     *
     * @param id
     */
    fun setPChoose(isAdas: Boolean, id: String) {
        val data = if (isAdas) allAdasAlarm else allAlarm
        data.forEach {
            if (id == it.id) {
                it.isChoose = isPChoose(isAdas, id)
                return
            }
        }
    }

    private var total = 0
    private var choose = 0
    private var totalAdas = 0
    private var chooseAdas = 0

    fun getTotal(isAdas: Boolean) = if (isAdas) totalAdas else total
    fun getChoose(isAdas: Boolean) = if (isAdas) chooseAdas else choose

    fun getChooseIds(isAdas: Boolean): String {
        if (isAdas) {
            chooseAdas = 0
        } else {
            choose = 0
        }
        var ids = ""
        if (isAdas) {
            allAdasAlarm.forEach {
                if (it.itemType == 2) {
                    if (it.isChoose) {
                        chooseAdas++
                        ids = "$ids,${it.id}"
                    }
                }
            }
        } else {
            allAlarm.forEach {
                if (it.itemType == 2) {
                    if (it.isChoose) {
                        choose++
                        ids = "$ids,${it.id}"
                    }
                }
            }
        }
        return if (ids.isNotEmpty()) ids.substring(1) else ""
    }

    /**
     * 父类是否全选
     *
     * @param pid
     * @return
     */
    private fun isPChoose(isAdas: Boolean, pid: String): Boolean {
        val data = if (isAdas) allAdasAlarm else allAlarm
        data.forEach {
            if (pid == it.pid && !it.isChoose) return false
        }
        return true
    }
}